package com.t.bricks.bussiness.controller.simpleuser;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.t.bricks.bussiness.db.entity.simpleuser.SimpleUser;
import com.t.bricks.bussiness.service.simpleuser.SimpleUserService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.Page;
import com.t.bricks.utils.function.data.StringUtil;

@Controller
//@Api("注释")
@RequestMapping("/simpleuser/")
public class SimpleUserController {
	
	@Autowired
	private SimpleUserService simpleUserService;
	
	/**
	 * 超级用户的密码
	 */
	@Value("${app.super.key:未配置app.super.key}")
	private String appSuperUserKey;
	
	/**
	 * 查询或刷新操作
	 * @param orderField
	 * @param orderDirection
	 * @param page
	 * @param sName
	 * @return
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/search")
	@ResponseBody
	//@ApiOperation("未加备注")
	public MsgEmity search(String orderField, String orderDirection,
			Integer pageCurrent, Integer pageSize, String sName) {
		Page page = new Page(pageCurrent, pageSize);
		String orderStr = StringUtil.append(orderField, " ", orderDirection);
		
		return simpleUserService.findByPage(sName, orderStr, page);
	}
	
	/**
	 * 删除用户
	 * @param httpSession
	 * @param iId
	 * @return
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/del")
	@ResponseBody
	//@ApiOperation("未加备注")
	public MsgEmity del(HttpSession httpSession, Integer iId) {
		if (null == iId || iId.intValue() < 1) {
			return new MsgEmity(false, "请选择删除的用户！");
		}
		
		return simpleUserService.del(iId);
	}
	
	/**
	 * 修改当前用户密码
	 * @param httpSession
	 * @param sOldPass
	 * @param sNewPass
	 * @return
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/edit/password")
	@ResponseBody
	//@ApiOperation("未加备注")
	public MsgEmity editPass(HttpSession httpSession, String sOldPass, String sNewPass) {
		SimpleUser simpleUser = (SimpleUser)httpSession.getAttribute("simpleUser");
		
		MsgEmity me = simpleUserService.editPass(simpleUser.getiId(), sOldPass, sNewPass);
		if (!me.isSuccess()) {
			return new MsgEmity(false, StringUtil.append("修改失败！<br>",me.getMsg()));
		}
		
		simpleUser = me.getData();
		httpSession.setAttribute("simpleUser", simpleUser);
		
		return new MsgEmity(true, "修改成功！");
	}
	
	/**
	 * 新增用户
	 * @param httpSession
	 * @param simpleUser
	 * @param model
	 * @return
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/save")
	@ResponseBody
	//@ApiOperation("未加备注")
	public MsgEmity save(HttpSession httpSession, @RequestBody SimpleUser simpleUser) {
		SimpleUser simpleUsers = (SimpleUser)httpSession.getAttribute("simpleUser");
		if (null == simpleUsers) {
			return new MsgEmity(false, "请重新登录！");
		}
		
		if (null == simpleUser.getiId() || simpleUser.getiId().intValue() < 1) {
			return simpleUserService.add(simpleUser);
		}
		
		return simpleUserService.edit(simpleUser, simpleUser.getsPass());
	}
	
	/**
	 * 根据编号取用户信息
	 * @param httpSession
	 * @param iId
	 * @return
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/find/id")
	@ResponseBody
	//@ApiOperation("未加备注")
	public MsgEmity findById(HttpSession httpSession, Integer iId) {
		SimpleUser simpleUsers = (SimpleUser)httpSession.getAttribute("simpleUser");
		if (null == simpleUsers) {
			return new MsgEmity(false, "请重新登录！");
		}
		
		return simpleUserService.findById(iId);
	}
	
	/**
	 * 根据关键值取对象
	 * @param httpSession
	 * @param sName
	 * @return
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/find/key")
	@ResponseBody
	//@ApiOperation("未加备注")
	public MsgEmity findByKey(HttpSession httpSession, String sName) {
		SimpleUser simpleUser = new SimpleUser();
		simpleUser.setsName(sName);
		return simpleUserService.findByKey(simpleUser);//根据关键值取对象
	}
	
	/**
	 * 重置密码
	 * @param iId
	 * @param sPass
	 * @return
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/reset/password")
	@ResponseBody
	//@ApiOperation("未加备注")
	public MsgEmity resetPass(String key, Integer iId) {
		if (null == key || (!key.equals(appSuperUserKey) && key.equals("未配置app.super.key"))) {
			return new MsgEmity(false, "操作秘钥错误!");
		}
		
		return simpleUserService.resetPass(iId);//重置密码
	}
	
}
